//--------------------------------------------------------------------------
// 
//  Copyright (c) Chili Software.  All rights reserved. 
// 
//  File: InspectedTrace.cs
//
//  Description: Stores information about recently inspected objects.
// 
//--------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace Chili.Opf3
{
	/// <summary>
	/// Class that stores information about recently inspected persistent objects.
	/// </summary>
	internal sealed class InspectedTrace
	{
		private IDictionary<int, object> _inspected = new Dictionary<int, object>();

		/// <summary>
		/// Adds an object to be traced as inspected by this class.
		/// </summary>
		/// <param name="obj">The object that is added to be traced by this instance of the 
		/// <see cref="InspectedTrace" /> class.</param>
		public void Add(object obj)
		{
			int key = GetKey(obj);
			_inspected.Add(key, obj);
		}

		/// <summary>
		/// Returns whether the persistent object is already in the trace.
		/// </summary>
		/// <param name="obj">The object that is checked of being already traced.</param>
		public bool Contains(object obj)
		{
			return _inspected.ContainsKey(GetKey(obj));
		}

		/// <summary>
		/// Clears the cache of the <see cref="PersistingTrace">PersistingTrace</see>.
		/// </summary>
		public void Clear()
		{
			_inspected.Clear();
		}

		/// <summary>
		/// Returns the key for the given object.
		/// </summary>
		/// <param name="obj">The object used to create the key.</param>
		private int GetKey(object obj)
		{
			return obj.GetHashCode();
		}

		/// <summary>
		/// Returns the amount of objects managed by this instance.
		/// </summary>
		public int Count
		{
			get { return _inspected.Count; }
		}
	}
}
